﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestProject
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           //Roles.AddUserToRole("admin","Admin");
            if(Roles.IsUserInRole("Admin"))
            {
                AddAdminMenuItem();
                AddEditorMenuItem();
                RemoveMenuItem("Login");
            }

            if(Roles.IsUserInRole("Editor"))
            {
                AddEditorMenuItem();
                RemoveMenuItem("Login");
            }
        }

        private void AddAdminMenuItem()
        {
            var addUsersMenuItem = new MenuItem();
            
            addUsersMenuItem.NavigateUrl = "~/Admin/UserManagment.aspx";
            addUsersMenuItem.Text = "Users managment";
            
            NavigationMenu.Items.Add(addUsersMenuItem);
        }

        private void AddEditorMenuItem()
        {
            var articlesMenuItem = new MenuItem();

            articlesMenuItem.NavigateUrl = "~/Editor/Articles.aspx";
            articlesMenuItem.Text = "My Articles";

            NavigationMenu.Items.Add(articlesMenuItem);
        }

        private void RemoveMenuItem(string itemName)
        {
            var menuItemToRemove = new MenuItem();
            foreach(MenuItem menuItem in NavigationMenu.Items)
            {
                if (menuItem.Text == itemName)
                    menuItemToRemove = menuItem;
            }
            NavigationMenu.Items.Remove(menuItemToRemove);
        }
    }

    
}
